package com.tengxing.e1.FactoryTest;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;
import java.util.Objects;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Button btn_MTQ_test, btn_DLL_test, btn_IMU_test, btn_SUS_test,
            btn_PPT_test, btn_TMP_test, btn_SADA_test, btn_LED_test,
            btn_LS_test, btn_More_test,
            btn_Ver_test, btn_stress_test, btn_stress_test_log,
            btn_Wireless_test, btn_cam_test, btn_speak_test;
    TextView tV_MTQ_TestResult, tV_DLL_TestResult, tV_IMU_TestResult, tV_SUS_TestResult,
            tV_PPT_TestResult, tV_TMP_TestResult, tV_SADA_TestResult, tV_LED_TestResult,
            tV_Info_TestResult, tV_info;


    SharedPreferences sharedPreferences;
    //SharedPreferences sharedPreferences = getSharedPreferences("Test_Result", MODE_PRIVATE);
    //SharedPreferences.Editor editor = sharedPreferences.edit();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Get_Permission();
        FactoryTest_Main_setResult_Init();
        /*try{


        }catch (Exception e){
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
            Log.e(TAG, e.getMessage());
        }*/
        Factory_Main_BTN();
        tV_info = findViewById(R.id.tV_info);
        GetRunningApps();
    }

    private void FactoryTest_Main_setResult_Init(){
        sharedPreferences = getSharedPreferences("Test_Result", MODE_PRIVATE);
        FactoryTest_Main_setResult(findViewById(R.id.tV_MTQ_TestResult), "MTQ");//磁力矩器测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_DLL_TestResult), "DLL");//动量轮测试
        FactoryTest_Main_setResult(findViewById(R.id.tV_IMU_TestResult), "IMU");//惯性测量单元测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_SUS_TestResult), "SUS");//太敏测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_PPT_TestResult), "PPT");//电推测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_TMP_TestResult), "TMP");//温度测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_SADA_TestResult), "SADA");//太阳翼测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_LED_TestResult), "LED");//LED测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_Ver_TestResult), "Ver");//版本号测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_LS_TestResult), "LS");//激光测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_Stress_TestResult), "Stress");//压力测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_Speak_TestResult), "Speak");//扬声器测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_Wireless_TestResult), "Wireless");//无线测试结果
        FactoryTest_Main_setResult(findViewById(R.id.tV_Cam_TestResult), "Cam");//相机测试结果
    }

    private void Factory_Main_BTN(){
        btn_MTQ_test = findViewById(R.id.btn_MTQ_test);
        btn_MTQ_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Toast.makeText(MainActivity.this, "MTQ_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, MTQ_Test_Activity.class);
                startActivity(intent);

            }
        });

        btn_DLL_test = findViewById(R.id.btn_DLL_test);
        btn_DLL_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "DLL_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, DLL_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_IMU_test = findViewById(R.id.btn_IMU_test);
        btn_IMU_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "IMU_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, IMU_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_SUS_test = findViewById(R.id.btn_SUS_test);
        btn_SUS_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "SUS_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, SUS_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_PPT_test = findViewById(R.id.btn_PPT_test);
        btn_PPT_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "PPT_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, PPT_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_TMP_test = findViewById(R.id.btn_TMP_test);
        btn_TMP_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "TMP_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, TMP_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_SADA_test = findViewById(R.id.btn_SADA_test);
        btn_SADA_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "SADA_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, SADA_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_LED_test = findViewById(R.id.btn_LED_test);
        btn_LED_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "LED_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, LED_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_Ver_test = findViewById(R.id.btn_Ver_test);
        btn_Ver_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Ver_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Ver_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_stress_test = findViewById(R.id.btn_stress_test);
        btn_stress_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Stress_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Stress_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_speak_test = findViewById(R.id.btn_speak_test);
        btn_speak_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Speak_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Speak_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_LS_test = findViewById(R.id.btn_LS_test);
        btn_LS_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "LS_Test", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, LS_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_stress_test_log = findViewById(R.id.btn_stress_test_log);
        btn_stress_test_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Stress_Test_Log_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_Wireless_test = findViewById(R.id.btn_Wireless_test);
        btn_Wireless_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Wireless_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });

        btn_cam_test = findViewById(R.id.btn_cam_test);
        btn_cam_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Camera_Test_Activity.class);
                startActivity(intent);
                finish();
            }
        });
        //com.un.factorykit/com.un.factorykit.framework.MainActivity
        btn_More_test = findViewById(R.id.btn_More_test);
        btn_More_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PackageManager packageManager = getPackageManager();
                //Intent intent = packageManager.getLaunchIntentForPackage("com.android.camera2");
                //Intent intent = packageManager.getLaunchIntentForPackage("com.un.factorykit");
                //Intent intent = packageManager.getLaunchIntentForPackage("com.android.dialer");
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*%23*%239527%23*%23"));//"tel:*#*#9527*#*#"//com.android.dialer
                //intent.setData(Uri.parse("tel:*#*#9527#*#*"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if(intent==null){
                    Toast.makeText(MainActivity.this, "未安装", Toast.LENGTH_LONG).show();
                }else{
                    startActivity(intent);
                }
            }
        });
    }

    private void GetRunningApps(){
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        if(activityManager!=null){
            StringBuilder runningInfo_Str = new StringBuilder();
            List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList;
            runningAppProcessInfoList = activityManager.getRunningAppProcesses();
            for(ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessInfoList){
                String pkgName = runningAppProcessInfo.processName;
                int importance = runningAppProcessInfo.importance;
                runningInfo_Str.append("pkg:").append(pkgName).append(" imp:").append(importance).append("\n");
            }
            tV_info.setText(runningInfo_Str);

        }
    }

    private void FactoryTest_Main_setResult(TextView textView, String key){
        String str_result = sharedPreferences.getString(key,"未测试");
        textView.setText(str_result);
        if(Objects.equals(str_result, "通过")){
            textView.setTextColor(0xFF00AA00);//绿色
        } else if (Objects.equals(str_result, "未通过")) {
            textView.setTextColor(0xFFAA0000);//红色
        }else{
        }
    }

    private void Get_Permission(){
        final int REQUEST_CAMERA_CODE = 100;
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_CODE);
            //return;
        }
        final int PERMISSION_REQUEST_LOCATION = 1;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_LOCATION);

        }
    }

}